/**
 * Created by admin on 2017/9/13.
 */
define([
    "helpers/network",
    "helpers/util",
    "views/modules/popup",
    "views/modules/form",
    "views/modules/pager",
    "models/user"
], function (Network, Util, PopupView, CommonForm, CommonPager, User) {

    var id = "";
    var keywordsId = id + "keywordsId";
    var tableId = id + "table";
    var TYPE = "User";
    var pagerId = id + "pager";
    var totalCountsId = id + "totalCounts";
    var pageIndex = 1, pageCounts;

    var onTableReady = function (page) {
        var obj = {
            params: {
                type: TYPE
            },
            page: page || 1,
            sort: ["creationTime,desc"]
        };
        Network.ajax("/BizView.queryAll.query", obj, function (ret) {
            $$(tableId).clearAll();
            $$(tableId).parse(ret.data.content);
            // closeWindow();
            // onTableReady();
            pageCounts = ret.data.totalPages;
            if (!page) {
                $$(pagerId).config.page = 0;
            }
            $$(pagerId).define("count", pageCounts || 1);
            $$(pagerId).refresh();
            $$(totalCountsId).setValue(ret.data.totalElements + "条数据, 共" + pageCounts + "页");

            _.forEach(ret.data.content, function (each) {
                var userObj = {
                    params: {
                        data: {userSnapshot: {id: each.data.id}},
                        type: "Order"
                    },
                    all: true
                };
                Network.ajax("/BizView.queryAll.query", userObj, function (ret) {
                    var newVal = $$(tableId).getItem(each.data.id);
                    newVal.data.orders = ret.data.content;
                    $$(tableId).updateItem(each.data.id, newVal);
                    $$(tableId).refresh()
                })
            });
        })
    };

    var genSearchFormUI = function () {
        return {
            id: keywordsId, view: "form", css: "toolbar", paddingY: 5, paddingX: 10,
            cols: [
                {},
                {
                    id: "static", name: "static", view: "select", width: 100, css: {"text-align": "center"},
                    value: "id",
                    options: User.userSearchTypes
                },
                CommonForm.genKeywordsUI(keywordsId, "依据左边状态查询", onTableReady),
                CommonForm.genQueryButtonUI(onTableReady)
            ]
        }
    };

    var genOperationFormUI = function () {
        return {
            view: "form", css: "toolbar", paddingY: 5, paddingX: 10,
            cols: [
                {view: "label", label: "操作"},
                // {
                //     view: "button", label: "重置登录密码", width: 90,
                //     // hidden: Authority.hasNoAuth("GOODS_MODIFY"),
                //     click: function () {
                //         if ($$("table").getSelectedItem()) {
                //             // showCreateAndUpdateWindow("设置", $$("table").getSelectedItem())
                //         }
                //     }
                // },
                {
                    id: "online", view: "button", value: "冻结/启动",
                    // hidden: Authority.hasNoAuth("GOODS_ONLINE"),
                    width: 90,
                    // click: online
                }
            ]
        }
    };

    var genDataTableUI = function () {
        return {
            id: tableId, view: "datatable", resizeColumn: true, select: "row", data: [],
            ready: onTableReady(),
            on: {
                onItemClick: function (id) {
                }
            },
            columns: [
                {
                    id: "id", header: "用户ID",
                    template: "#data.id#"
                    // header: {text: "id", css: {"text-align": "center"}},
                    // css: {"text-align": "center"}
                },
                {
                    id: "nickname", header: "昵称", fillspace: true,
                    template: "#data.nickname#"
                },
                {
                    id: "mobile", header: "手机号", fillspace: true,
                    template: "#data.mobile#"
                },
                {
                    id: "status", header: "账户状态", fillspace: true,
                    template: "#data.status#"
                },
                {
                    id: "id3", header: "订单数", fillspace: true,
                    template: function (obj) {
                        return obj.data.orders ? obj.data.orders.length : 0
                    }
                },
                {
                    id: "creationTime", header: "注册时间", fillspace: true,
                    template: "#data.creationTime#"
                },
                {
                    id: "loginTime", header: "上次登录", fillspace: true,
                    template: function (obj) {
                        if (!obj.data.loginTime) return "";
                        return Util.getFormattedDateTime("yyyy-MM-dd hh:mm:ss", new Date(obj.data.loginTime))
                    }
                }
            ]
        }
    };

    var genFooterUI = function () {
        return {
            cols: [
                CommonPager.genPagerUI(pagerId, pageIndex, pageCounts, onTableReady),
                CommonPager.genTotalCountsUI(totalCountsId), {}
            ]
        }
    };

    var layout = {
        padding: 10,
        rows: [
            genSearchFormUI(),
            genOperationFormUI(),
            genDataTableUI(),
            genFooterUI()
        ]
    };

    return {$ui: layout};

});